import type { Router } from 'vue-router';

import { ref, onMounted } from 'vue';
import { useRouter, onBeforeRouteUpdate } from 'vue-router';

export function usePageTitle(_router?: Router) {
  const title = ref('注册下载运天下');
  const leftArrow = ref(true)
  let router: Router = useRouter();
  if (_router) {
    router = _router;
  }

  function modifyLeftArrow(isLogin: boolean) {
    leftArrow.value = !isLogin
  }

  onBeforeRouteUpdate((to, from) => {
    console.log(to,from)
    if (typeof to.name === 'string') title.value = to.name;

    // 修改左侧导航按钮
    modifyLeftArrow(to.path === '/home/index');

  });

  onMounted(() => {
    // 修改navbar title
    if (typeof router.currentRoute.value.name === 'string')
      title.value = router.currentRoute.value.name;
    console.log(router)
    // 修改左侧导航按钮
    modifyLeftArrow(router.currentRoute.value.path === '/home/index')

  });

  return {
    title,
    leftArrow
  };
}
